//
//  ChannelViewController.m
//  ipadclient
//
//  Created by Stéphane Prohaszka on 24/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ChannelViewController.h"

#import "ParleysService.h"
#import "Space.h"

@implementation ChannelViewController

@synthesize space;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //Init
    tableRows = [[NSMutableArray alloc] init];
    
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateTableWithChannels)
                                                 name:@"ChannelsLoaded"
                                               object:nil];
    
    [[ParleysService sharedService] loadChannelsFromSpace:space];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableRows count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ChannelIdentifier = @"Channel";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChannelIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ChannelIdentifier] autorelease];
    }
    
    ParleysSection* section = (ParleysSection*)[tableRows objectAtIndex:[indexPath row]];
    cell.textLabel.text = section.name;
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    Channel* channel = (Channel*)[tableRows objectAtIndex:[indexPath row]];
    [[ParleysService sharedService] loadPresentationsFromChannel:channel];
}

#pragma mark -
#pragma mark Custom
- (void)updateTableWithChannels {
    tableRows = [ParleysService sharedService].channels;
    [self.tableView reloadData];
}

@end
